package com.quanyan.ic.video.http.service.impl;

import com.alibaba.fastjson.JSON;
import com.quanyan.ic.video.http.cache.Hik8710EventDAO;
import com.quanyan.ic.video.http.service.PlaceUnitAlarmService;
import com.quanyan.ic.video.model.hik8710.Hik8710Event;
import com.quanyan.ic.video.model.hik8710.Hik8710EventAlarm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zhuo.shi on 2017/8/21.
 */
@Service
public class PlaceUnitAlarmServiceImpl implements PlaceUnitAlarmService {

    public static final Logger logger = LoggerFactory.getLogger(PlaceUnitAlarmServiceImpl.class);

    @Autowired
    private Hik8710EventDAO hik8710EventDAO;

    /**
     * 添加或者更新告警信息
     */
    @Override
    public Hik8710EventAlarm addOrUpdateAlarm(int placeId, int placeUnitId, Hik8710Event event, long startTime, long endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Hik8710EventAlarm generateAlarm = null;

        Hik8710EventAlarm oldAlarm = hik8710EventDAO.getEventAlarm(placeId, placeUnitId, startTime);
        boolean needNew = false;
        if (oldAlarm != null) {
            try {
                if (endTime <= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(oldAlarm.getEndTime()).getTime()) {
                    return null;         //排除脏乱数据，当前告警结束时间 <= 所有已存在的告警中最大的结束时间
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String oldEndTime = oldAlarm.getEndTime();
            if (oldAlarm.getStartTime().equals(sdf.format(new Date(startTime)))) {
                oldAlarm.setEndTime(sdf.format(new Date(endTime)));
                if (!oldAlarm.getEventId().equals(event.getEvent_id())){            //如果此告警中的eventId不等于当前eventId，放进此告警的eventId列表去
                    if (!oldAlarm.getEventIds().contains(event.getEvent_id())) {
                        oldAlarm.getEventIds().add(event.getEvent_id());
                    }
                }
                oldAlarm.setEventId(event.getEvent_id());
                hik8710EventDAO.addOrUpdateEventAlarm(placeId, placeUnitId, startTime, oldAlarm);
                generateAlarm = oldAlarm;
                logger.info("alarm: update the old alarm st: {}, et: {} --> {}, {}", oldAlarm.getStartTime(), oldEndTime, sdf.format(new Date(endTime)), JSON.toJSONString(oldAlarm));
            } else {
                logger.info("the old alarm start time not equals, new alarm");
                needNew = true;
            }
        }
        if (oldAlarm == null || needNew) { //需要新增一个告警
            Hik8710EventAlarm alarm = new Hik8710EventAlarm();
            alarm.setPlaceId(placeId);
            alarm.setPlaceUnitId(placeUnitId);
            alarm.setPlaceUnitName(event.getRes_name());
            alarm.setEventId(event.getEvent_id());
            alarm.setRes_index(event.getRes_index());  //摄像头编号
            alarm.setStartTime(sdf.format(new Date(startTime)));
            alarm.setEndTime(sdf.format(new Date(endTime)));
            alarm.setUuid(hik8710EventDAO.getEventAlarmUuid());
            alarm.getEventIds().add(event.getEvent_id());
            hik8710EventDAO.addOrUpdateEventAlarm(placeId, placeUnitId, startTime, alarm);
            generateAlarm = alarm;
            logger.info("alarm: add a new alarm {}", JSON.toJSONString(alarm));
        }
        return generateAlarm;
    }
}
